package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import model.PropPojo;

public class PropDao {
	
	public void insertPropPojo(PropPojo proppojo) {
System.out.println("Going to insert a property");
		DBCon db = new DBCon();
		Connection con;
		try {
			con = db.getCon();
		
			//First check that an identical record doesn't already exist
		
		if(!doesPropExist(proppojo)){
			Statement stmt;
				stmt = con.createStatement();
				stmt.executeUpdate("INSERT INTO predicates(pre_name, pre_uri, pre_ns) values('"+proppojo.getPropName()+"','"+proppojo.getPropUri()+"','"+proppojo.getPropNamespace()+"')");
System.out.println("Inserted into properties table");
		}
		else{
			Statement stmt;
			stmt = con.createStatement();
			stmt.executeUpdate("UPDATE predicates SET pre_name='"+proppojo.getPropName()+"', pre_uri='"+proppojo.getPropUri()+"', pre_ns='"+proppojo.getPropNamespace()+"' WHERE pre_uri='"+proppojo.getPropUri()+"'");			
		}
		
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public boolean doesPropExist(PropPojo proppojo) {
		boolean doesItExist=true;
		
		DBCon db = new DBCon();
		Connection con;
		try {
			con = db.getCon();
		
		String propName = proppojo.getPropName();
		String propUri = proppojo.getPropUri();
		String propNs = proppojo.getPropNamespace();
	
System.out.println("doesPropExist: "+propUri);

			Statement stmt;
			stmt = con.createStatement();
		
		ResultSet rs = stmt.executeQuery("SELECT pre_id FROM predicates WHERE pre_uri='"+propUri+"'");
			if(rs.next()){
				doesItExist=true;
			}
			else{
				doesItExist=false;
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return doesItExist;
	}
	
	public int getPropId(String uri) {

		int id = -1;
		try {

			DBCon db = new DBCon();
			Connection con;

			con = db.getCon();

			Statement stmt;
			stmt = con.createStatement();
			ResultSet rs;
			rs = stmt
					.executeQuery("SELECT pre_id FROM predicates WHERE pre_uri='"
							+ uri+"'");
			while (rs.next()) {
				id = rs.getInt("pre_id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
	}


}
